package com.atguigu.ucenterservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.commonutils.jwt.JwtUtils;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.entity.vo.RegistVo;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 老王
 * @since 2020-05-23
 */
@Api(tags = "登陆相关接口")
@RestController
@RequestMapping("/eduucenter/ucenter")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;


    @ApiOperation("手机号密码登陆")
    @PostMapping("/login")
    public R login(@RequestBody UcenterMember ucenterMember){
        //调用service
        String token=ucenterMemberService.login(ucenterMember);
        return R.ok().data("token", token);
    }

    @ApiOperation("手机密码注册")
    @PostMapping("/register")
    public R register(@RequestBody RegistVo registVo){
        ucenterMemberService.regist(registVo);
        return R.ok();
    }

    @ApiOperation("根据token获取用户信息")
    @GetMapping("getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        //jwt解析出id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember userInfo = ucenterMemberService.getById(id);
        return R.ok().data("userInfo", userInfo);
    }
}

